package com.kinghunt.misp.service.impl.devicex;

import com.bygoodtech.kings.base.utils.ConvertUtil;
import com.bygoodtech.kings.common.dto.ApiResult;
import com.bygoodtech.kings.common.dto.page.PageInfo;
import com.kinghunt.misp.common.dto.devicex.DeviceDTO;
import com.kinghunt.misp.common.dto.devicex.DeviceUsageDTO;
import com.kinghunt.misp.common.enums.devicex.DeviceTypeEnum;
import com.kinghunt.misp.common.param.devicex.DeviceUsageParam;
import com.kinghunt.misp.dal.dao.devicex.DeviceUsageDAO;
import com.kinghunt.misp.dal.model.devicex.DeviceUsageDO;
import com.kinghunt.misp.service.api.devicex.DeviceService;
import com.kinghunt.misp.service.api.devicex.DeviceUsageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 设备使用管理
 *
 * @Author ciaran
 * @Date 2023/7/13 21:58l
 */
@Slf4j
@Service
@Transactional
public class DeviceUsageServiceImpl implements DeviceUsageService {

    @Autowired(required = false)
    private DeviceUsageDAO deviceUsageDAO;
    @Autowired(required = false)
    private DeviceService deviceService;
    @Override
    public ApiResult<PageInfo<DeviceUsageDTO>> getDeviceUsageList(DeviceUsageParam deviceUsageParam) {
        int offset = (deviceUsageParam.getPageNum() - 1) * deviceUsageParam.getPageSize();
        DeviceUsageDO deviceUsageDO = ConvertUtil.convert(deviceUsageParam, DeviceUsageDO.class);
        int count = deviceUsageDAO.selectDeviceUsageListCount(deviceUsageDO);
        PageInfo<DeviceUsageDTO> pageInfo = new PageInfo<>();
        pageInfo.setTotal(count);
        pageInfo.setPageNum(deviceUsageParam.getPageNum());
        pageInfo.setPageSize(deviceUsageParam.getPageSize());
        List<DeviceUsageDO> deviceUsageDOList = deviceUsageDAO.selectDeviceUsageList(deviceUsageDO, offset, deviceUsageParam.getPageSize());
        List<DeviceUsageDTO> dto =  ConvertUtil.convert(deviceUsageDOList, DeviceUsageDTO.class);
        pageInfo.setData(dto);
        return ApiResult.ok(pageInfo);
    }

    @Override
    public ApiResult<DeviceUsageDTO> getDeviceUsageDetail(Long repairId) {
        DeviceUsageDO deviceUsageDO = deviceUsageDAO.selectByPrimaryKey(repairId);
        DeviceUsageDTO deviceUsageDTO = ConvertUtil.convert(deviceUsageDO, DeviceUsageDTO.class);
        return ApiResult.ok(deviceUsageDTO);
    }

    @Override
    public ApiResult addDeviceUsage(DeviceUsageParam deviceUsageParam) {
        ApiResult<DeviceDTO> apiResult= deviceService.getDeviceDetail(deviceUsageParam.getDeviceId());
        if(!apiResult.isSuccess() || null == apiResult.getData() ){
            return ApiResult.failOfMessage("您申请使用的设备不存在！");
        }
        if(apiResult.getData().getStatus() != DeviceTypeEnum.DEVICE_TYPE_1.getValue() ){
            log.error("您申请使用的设备状态为！, {} ,!", DeviceTypeEnum.DEVICE_TYPE_1.getDesc());
            return ApiResult.failOfMessage("您申请使用的设备状态为非启用状态，不能申请使用!");
        }
        DeviceUsageDO deviceUsageDO = ConvertUtil.convert(deviceUsageParam, DeviceUsageDO.class);
        // 保存地址到数据库
        deviceUsageDAO.insert(deviceUsageDO);
        return ApiResult.ok();
    }
}
